package cn.lizhangqu.kb.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONException;

import java.util.List;

import cn.lizhangqu.kb.R;
import cn.lizhangqu.kb.activity.ReportDetailActivity;
import cn.lizhangqu.kb.adapter.CommonAdapter;
import cn.lizhangqu.kb.adapter.ReportAdapter;
import cn.lizhangqu.kb.api.LgrgApi;
import cn.lizhangqu.kb.api.LgrgUtil;
import cn.lizhangqu.kb.handler.ArticlesListDataResponseHandler;
import cn.lizhangqu.kb.model.Report;
import cn.lizhangqu.kb.ui.basefragment.BaseSwipeRefreshFragment;
import cn.lizhangqu.kb.util.Toasts;

/**
 * Created by 柯尊诚 on 2015/11/6.
 * kzc
 */
public class ReportFragment extends BaseSwipeRefreshFragment<Report>{

    //private ReportJsonResponseHandler handler = new ReportJsonResponseHandler();

    public final static int TYPE_XYXW = 1000;
    public final static int TYPE_TZGG = 1001;
    public final static int TYPE_JXJW = 1002;
    public final static int TYPE_TXHD = 1003;
    public final static int TYPE_ZPXX = 1004;
    public final static int TYPE_ZDJS = 1005;
    public final static int TYPE_XGDT = 1006;
    public static int type = 0;

    ArticlesListDataResponseHandler handler = new ArticlesListDataResponseHandler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        type = getActivity().getIntent().getIntExtra("type", 0);
        Log.d("type", type+"");
    }

    @Override
    public CommonAdapter<Report> getAdapter() {
        return new ReportAdapter(getActivity(), R.layout.list_item_report);
    }

    @Override
    public List<Report> getDatas(byte[] responeString) {
        try {
             handler.parse(new String(responeString));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return handler.getParsedItem().atArticles;
    }

    @Override
    public void requestData() {
        switch (type) {
            case TYPE_XYXW :
                LgrgApi.getSchoolMore(mCurrentPage, LgrgUtil.XYXW, mHandler);break;
            case TYPE_TZGG :
                LgrgApi.getSchoolMore(mCurrentPage, LgrgUtil.TZGG, mHandler);break;
            case TYPE_JXJW :
                LgrgApi.getSchoolColumn(mCurrentPage, LgrgUtil.JXJW, mHandler);break;
            case TYPE_TXHD :
                LgrgApi.getSchoolColumn(mCurrentPage, LgrgUtil.TXHD, mHandler);break;
            case TYPE_ZPXX :
                LgrgApi.getSchoolColumn(mCurrentPage, LgrgUtil.ZPXX, mHandler);break;
            case TYPE_ZDJS :
                LgrgApi.getSchoolColumn(mCurrentPage, LgrgUtil.ZDJS, mHandler);break;
            case TYPE_XGDT :
                LgrgApi.getStudentWorker(mCurrentPage, LgrgUtil.XGDT, mHandler);break;
        }
    }

    @Override
    public void onItemClick(int position, Report report) {
        Toasts.makeText(report.getTitle());
        Intent intent = new Intent(getActivity(), ReportDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("report",report);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
